package controller

import (
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

type TestController struct {
}
type Test struct {
	Test string `json:"test"`
}

func (t TestController) TestRouter(ginServer *gin.Engine) {
	store := cookie.NewStore([]byte("testSession"))
	ginServer.Use(sessions.Sessions("t", store))
	group := ginServer.Group("/test")
	{

		group.GET("/hello", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"msg": "hello world",
			})
		})
		group.POST("/json", func(ctx *gin.Context) {

			var test Test
			ctx.BindJSON(&test)
			_ = ctx.ShouldBindBodyWith(&test, binding.JSON)
			fmt.Printf("inteceptor %v\n", test)
			ctx.Next()
		}, t.testJsonObject)
	}
}
func (t TestController) testJsonObject(ctx *gin.Context) {
	var test Test
	_ = ctx.ShouldBindBodyWith(&test, binding.JSON)
	ctx.BindJSON(&test)
	fmt.Printf("controller %v\n", test)
}
